package com.simpleaccount;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

import com.simpleaccount.Common.BaseActivity;

/**
 * Created by Administrator on 2017/7/10.
 */

public class SplashActivity extends BaseActivity {

    private static final int FAILURE = 0; // 失败
    private static final int SUCCESS = 1; // 成功
    private static final int OFFLINE = 2; // 如果支持离线阅读，进入离线模式
    private static final int SHOW_TIME_MIN = 800;

    private TextView mVersionNameText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);

        mVersionNameText = (TextView) findViewById(R.id.version_name);
        //mVersionNameText.setText(.mVersionName);

        new AsyncTask<Void, Void, Integer>() {

            @Override
            protected Integer doInBackground(Void... params) {
                int result;
                long startTime = System.currentTimeMillis();
                result = loadingCache();
                long loadingTime = System.currentTimeMillis() - startTime;
                if (loadingTime < SHOW_TIME_MIN) {
                    try {
                        Thread.sleep(SHOW_TIME_MIN - loadingTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return result;
            }

            @Override
            protected void onPostExecute(Integer result) {

                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

                SharedPreferences preferences= getSharedPreferences("count", 0); // 存在则打开它，否则创建新的Preferences
                int count = preferences.getInt("count", 0); // 取出数据

                /**
                 *如果用户不是第一次使用则直接调转到显示界面,否则调转到引导界面
                 */
                if (count == 0) {
                    Intent intent1 = new Intent();
                    intent1.setClass(SplashActivity.this, WelcomeActivity.class);
                    startActivity(intent1);
                } else {
                    Intent intent2 = new Intent();
                    intent2.setClass(SplashActivity.this, MainActivity.class);
                    startActivity(intent2);
                }
                finish();

                //实例化Editor对象
                SharedPreferences.Editor editor = preferences.edit();
                //存入数据
                editor.putInt("count", 1); // 存入数据
                //提交修改
                editor.commit();

            }
        }.execute(new Void[]{});
    }

    private int loadingCache() {
        if (false) {
            return OFFLINE;
        }
        return SUCCESS;
    }


}
